package com.wk.service;

import com.wk.dao.CommentDao;
import com.wk.dao.ReadDao;
import com.wk.vo.Book;
import com.wk.vo.Chapters;
import com.wk.vo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ReadService {
    @Autowired(required = false)
    private ReadDao readDao;

    @Resource
    private CommentService commentService;

    public List<Chapters> getAllChapters(int bookId){
        List<Chapters> result = new ArrayList<>();
        try {
            result = readDao.getAllChapters(bookId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public HashMap<String,Object> getCurrentChapter(int bookId, int userId){
        HashMap<String,Object> result = new HashMap<>();
        try {
            Comment comment = readDao.getLastLocation(bookId, userId);
            if (null != comment){
                Integer lastLocation = comment.getLastLocation();
                if (null != lastLocation){
                    String lastTime = comment.getLastTime();
                    String chapterName = readDao.getChapterByChapterId(lastLocation).getChapterName();
                    result.put("lastLocation", lastLocation);
                    result.put("chapterName", chapterName);
                    result.put("lastTime", lastTime);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List<Book> getAllBooks(){
        List<Book> result = new ArrayList<>();
        try {
            result = readDao.getAllBooks();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public HashMap<String,String> readBook(int bookId, int userId, int chapterId){
//        Chapters result = new Chapters();
        HashMap<String,String> result = new HashMap<>();
        result.put("chapterName", readDao.getChapterByChapterId(chapterId).getChapterName());
        try {
//            result = readDao.getChapterByChapterId(chapterId);
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            result.put("lastTime", formatter.format(date));
            if (commentService.selectUserId(bookId,userId)){
                //更新阅读记录
                System.out.println("update");
                readDao.updateLog(bookId, userId, formatter.format(date), chapterId);
            } else {
                //添加阅读记录
                System.out.println("add");
                readDao.addLog(bookId, userId, formatter.format(date), chapterId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
